<?php

namespace app\controllers\h5;

use app\controllers\h5\base\H5Controller;
use app\models\comment\Comment;
use app\models\comment\CommentImage;
use app\models\order\Order;
use app\models\order\OrderItem;
use app\models\system\SetApp;
use app\services\Upload as Service;
use yii\helpers\Html;

class CommentController extends H5Controller
{
    // upload image
    public function actionUpload()
    {
        $result = Service::uploadImage('comment');
        if ($result) {
            return $this->success($result, '上传成功');
        } else {
            return $this->fail('上失败,' . Service::getLastError());
        }
    }

    // check comment
    public function actionCheck()
    {
        $set = SetApp::getLastOne();
        if ($set->open_comment == 0) {
            return $this->success(['is' => false, 'message' => '评论已经关闭,无法评论']);
        }

        $id = $this->post('id');
        // $model = Comment::findOne(['order_id' => $id, 'user_id' => $this->user->id]);
        $count = Comment::find()
            ->where(['user_id' => $this->user->id])
            ->andWhere(['order_id' => $id])
            ->count();
        // $max = 3;
        $max = 1;
        if ($count && $count >= $max) {
            return $this->success(['is' => false, 'message' => '您已经评论过了(或者评论超过了'.$max.'条)']);
        }

        return $this->success(['is' => true]);
    }

    public function actionSave()
    {
        $id = $this->post('id');
        $content = Html::encode($this->post('content'));
        $images = $this->post('images');
        if (!$content || !$images) {
            return $this->fail('请填写完整');
        }

        $order = Order::findOne(['id' => $id]);

        if (!$order) {
            return $this->fail('订单错误');
        }

        $order->status = Order::$statusMapping['FINISH'];
        $order->save();

        // product
        $product_id = OrderItem::find()
            ->where(['order_id' => $order->id])
            ->select(['product_id'])
            ->scalar();

        $model = new Comment();
        $model->content = $content;
        $model->user_id = $this->user->id;
        $model->order_id = $id;
        $model->check = 0;
        $model->product_id = $product_id;
        $model->save();

        foreach ($images as $image) {
            $cmodel = new CommentImage();
            $cmodel->comment_id = $model->id;
            $cmodel->image_id = $image;
            $cmodel->save();
        }

        return $this->success();
    }

    public function actionList()
    {
        $page = intval($this->post('page', $this->page));
        $pageSize = intval($this->post('pageSize', $this->pageSize));

        $query = Comment::find()
            ->where(['user_id' => $this->user->id]);

        $totalCount = $query->count();
        $totalPage = ceil($totalCount / $pageSize);

        $list = $query->orderBy(['id' => SORT_DESC])
            ->offset(($page - 1) * $pageSize)
            ->limit($pageSize)
            ->with(['images', 'images.image'])
            ->asArray()
            ->all();

        foreach ($list as &$item) {
            $item['content'] = Html::decode($item['content']);
        }

        return $this->successEncode([
            $this->list => $list,
            $this->totalCount => (int)$totalCount,
            $this->totalPage => $totalPage,
            $this->currentPage => $page
        ]);
    }

    public function actionListByProduct()
    {
        $product_id = $this->post('product_id');
        $limit = $this->post('limit');

        $list = Comment::find()
            ->where(['product_id' => $product_id])
            ->orderBy(['id' => SORT_DESC])
            ->with(['images', 'images.image', 'user'])
            ->limit($limit)
            ->asArray()
            ->all();

        foreach ($list as &$item) {
            // content
            $item['content'] = Html::decode($item['content']);

            // images
            $i = [];
            if ($item['images']) {
                foreach ($item['images'] as $image) {
                    array_push($i, $image['image']['path']);
                }
            }

            $item['path'] = $i;

            // user
            if ($item['user']['nickname']) {
                $item['user']['nickname'] = base64_decode($item['user']['nickname']);
            }
        }

        return $this->successEncode(['list' => $list]);
    }
}